package com.gelsanalyzer.ui.swing.command;

import java.awt.event.*;
import java.beans.*;

import javax.swing.*;

import com.gelsanalyzer.mvc.controller.*;

public class ExitCommand extends AbstractAction implements Command {

    private IController controller;

    public ExitCommand(String label, IController controller) {
        this(label, null, null, null, controller);

    }

    public ExitCommand(String label,
                       ImageIcon icon,
                       String desc,
                       Integer mnemonic,
                       IController controller) {
        super(label, icon);
        putValue(SHORT_DESCRIPTION, desc);
        putValue(MNEMONIC_KEY, mnemonic);
        this.controller = controller;
    }

    public void Execute() {

    }

    private int promptForSave() {
        int i = JOptionPane.showConfirmDialog(null,
            "Would you like to save before exiting the application?",
            "Save before exit?", JOptionPane.YES_NO_CANCEL_OPTION);
        return i;
    }

    public void actionPerformed(ActionEvent e) {
        // ask the user if they want to save their progress if it is not
        // already saved...
        int i = promptForSave();
        switch ( i ) {
            case JOptionPane.YES_OPTION:
                controller.propertyChange(new PropertyChangeEvent(this,
                    AbstractController.APP_SAVE_MODELS, null, null));
                System.exit(0);
                break;
            case JOptionPane.CANCEL_OPTION:
                break;
            case JOptionPane.NO_OPTION:
            default:
                System.exit(0);
                break;
        }
    }

}
